{extend name='layout/base'}
{block name='header'}
<!--加载html编辑器的css以及js-->
<link rel="stylesheet" type="text/css" href="__PUBLIC__/static/simditor-2.3.9\site\assets\styles\simditor.css" />

<script type="text/javascript" src="__PUBLIC__/static/simditor-2.3.9\site\assets\scripts/module.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/simditor-2.3.9\site\assets\scripts/hotkeys.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/simditor-2.3.9\site\assets\scripts/uploader.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/simditor-2.3.9\site\assets\scripts/simditor.js"></script>
{/block}
{block name='title'}商品管理-添加商品{/block}
{block name='left'}
{include file="layout/goodsMenu" /}
{/block}
{block name='right'}
<div class="page-header">
    <h1>{$item_title}</h1>
</div>
<form class="form-horizontal" action="<?php echo url('admin/admin.goods.GoodsAdd/save');?>" method="post" enctype="multipart/form-data">
    <!--编写隐藏表单，用于在编辑模式下记录商品的id-->
    <input type="hidden" name="goods_id" value="{$item->id}">
    <!--商品标题start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">商品标题</label>
        <div class="col-sm-10">
            <input type="text" name="goods_title" class="form-control" id="goods_title" placeholder="请输入商品标题" value="{$item->title}">
        </div>
    </div>
    <!--商品标题end-->  
    <!--商品类别、口味start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">类别</label>
        <div class="col-sm-4">
            <select class="form-control" name="goods_category" id="goods_category">
                <?php
                    $config_status = config('set.category_status');
                    foreach($config_status as $k => $v):
                        //处理商品编辑模式下商品类别的默认选择
                        $selected = (string)$k === (string)$item_classification->category ? 'selected' : '';
                ?>
                <option value="{$k}" {$selected}>{$v}</option>
                <?php endforeach?>
            </select> 
        </div>
        <label for="inputEmail3" class="col-sm-2 control-label">口味</label>
        <div class="col-sm-4">
            <select class="form-control" name="goods_taste" id="goods_taste">
                <?php
                    $config_status = config('set.taste_status');
                    foreach($config_status as $k => $v):
                        //处理商品编辑模式下商品口味的默认选择
                        $selected = (string)$k === (string)$item_classification->taste ? 'selected' : '';                        
                ?>
                <option value="{$k}" {$selected}>{$v}</option>
                <?php endforeach?>
            </select> 
        </div>        
    </div>
    <!--商品类别、口味end-->  
    <!--商品特色、规格start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">特色</label>
        <div class="col-sm-4">
            <select class="form-control" name="goods_features" id="goods_features">
                <?php
                    $config_status = config('set.features_status');
                    foreach($config_status as $k => $v):
                        //处理商品编辑模式下商品特色的默认选择
                        $selected = (string)$k === (string)$item_classification->features ? 'selected' : '';                        
                ?>
                <option value="{$k}" {$selected}>{$v}</option>
                <?php endforeach?>
            </select> 
        </div>
        <label for="inputEmail3" class="col-sm-2 control-label">规格</label>
        <div class="col-sm-4">
            <select class="form-control" name="goods_size" id="goods_size">
                <?php
                    $config_status = config('set.size_status');
                    foreach($config_status as $k => $v):
                        //处理商品编辑模式下商品类别的默认选择
                        $selected = (string)$k === (string)$item_classification->size ? 'selected' : '';                        
                ?>
                <option value="{$k}" {$selected}>{$v}</option>
                <?php endforeach?>
            </select> 
        </div>        
    </div>
    <!--商品特色、规格end-->        
    <!--商品价格、商品市场价格、商品库存start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">商品价格</label>
        <div class="col-sm-2">
            <input type="text" name="goods_price" class="form-control" id="goods_price" placeholder="价格" value="{$item_attr->price}">
        </div>
        <label for="inputEmail3" class="col-sm-2 control-label">市场价格</label>
        <div class="col-sm-2">
            <input type="text" name="goods_market_price" class="form-control" id="goods_market_price" placeholder="市场价" value="{$item_attr->market_price}">
        </div>
        <label for="inputEmail3" class="col-sm-2 control-label">库存数量</label>
        <div class="col-sm-2">
            <input type="text" name="goods_stock_number" class="form-control" id="goods_stock_number" placeholder="库存数" value="{$item_attr->stock_number}">
        </div>              
    </div>
    <!--商品价格、商品市场价格、商品库存end--> 
    <!--商品简介start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">商品简介</label>
        <div class="col-sm-10">
            <input type="text" name="goods_description" class="form-control" id="goods_description" placeholder="请输入商品信息" value="{$item->description}">
        </div>
    </div>
    <!--商品简介end--> 
    <!--商品状态start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">商品状态</label>
        <div class="col-sm-10">
            <?php
                //读取extra/set.php中的配置
                $config_status = config('set.goods_status');
                foreach($config_status as $key => $value):
                    //处理编辑模式下商品状态的默认选择
                    $checked = (string)$key === (string)$item_attr->status ? 'checked' : '' ;
            ?>            
            <label class="radio-inline">
                <input type="radio" name="goods_status" value="{$key}" {$checked}>{$value}
            </label>
            <?php endforeach?>            
        </div>
    </div>
    <!--商品状态end--> 
    <!--上传图片start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">上传图片</label>
        <div class="col-sm-10" id="file_list">
            <button type="button" id="add_image" class="btn btn-default btn-sm" style="margin:0 8px 10px 0;">添加图片</button>
            <span class="text-danger">默认第一张图为商品主图，请谨慎上传！</span>           
        </div>        
    </div>
    <!--上传图片end-->
    <!--已上传图片start-->
    <?php if($item_pictures):?>
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">已上传图片</label>
        <div class="col-sm-10">
        <?php foreach($item_pictures as $image):?>
            <a href="<?php echo url('admin/admin.goods.GoodsAdd/imageDel',['goods_id'=>$item->id,'image_id'=>$image->id]);?>" onclick="return confirm('确认删除吗？')" style="text-decoration: none;">
            <img src="<?php echo thumb_url($image['path']);?>" style="width: 80px" class="img-rounded"/>
            <span class="glyphicon glyphicon-remove text-danger" style="position: relative; top: -30px; left: -20px;"></span>            
            </a>                        
        <?php endforeach?>              
        </div>       
    </div>
    <?php endif;?>
    <!--已上传图片end-->      
    <!--商品介绍start-->
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">商品介绍</label>
        <div class="col-sm-10">
            <textarea name="goods_content" class="form-control" id="goods_content" placeholder="请输入商品介绍……" autofocus>{$item->content}</textarea>
            <script type="text/javascript">
                var editor = new Simditor({
                        textarea: $('#goods_content'),
                        toolbar:[
                            'title',
                            'bold',
                            'italic',
                            'underline',
                            'strikethrough',
                            'fontScale',
                            'color',
                            'ol',
                            'ul',
                            'blockquote',
                            'table',
                            'link',
                            'image',
                            'hr',
                            'indent',
                            'outdent',
                            'alignment',    
                        ],
                        //对图片上传的处理,url和fileKey是必须的参数，fileKey代表上传文件的名字
                        upload:{
                            url:'<?php echo url('admin/admin.goods.GoodsAdd/uploadImg');?>',
                            fileKey:'simditor_files',
                        },                   
                    });
            </script>
        </div>
    </div>
    <!--商品介绍end--> 
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" name="r_button" value="r_button" class="btn btn-primary">提交</button>
        </div>
    </div>                      
</form> 

<!-- 多图上传功能 -->
<div id="file_tpl" style="display: none;">
    <div>
        <div class="col-sm-10" style="margin-bottom: 10px;">
            <input type="file" name="goods_files[]">
        </div>
        <div class="col-sm-2">
            <button type="button" id="add_image" class="btn btn-danger btn-xs delete_image">删除</button>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $("#add_image").click(function(){
            var html = $("#file_tpl").html();
            $("#file_list").append(html);
        });
        $("#file_list").on("click",".delete_image",function(){
            //这里的this代表.delete_image
            $(this).parent().parent().remove();
        });
        //当文档加载完之后自动点击
        $("#add_image").click();
    });
</script>     
{/block}